/**
 * 球类
 * Created by zhd on 2017/6/23.
 */
var BallSprite = cc.Sprite.extend({
    _is_roll: false,// 是否处于滚动状态
    _routes: [],//滚动路径
    _step: 0,//初始步数
    _step_speed: 0,//滚动速度

    /**
     * ctor
     * @param {number} index GC.balls下标
     */
    ctor: function (index) {
        this._super();
        this.loadInit(index);
    },

    /**
     * 初始化
     * @param {number} index
     */
    loadInit: function (index) {
        this._tag = GC.balls[index].tag;
        this._name = GC.balls[index].name;
        this.setSpriteFrame(this._name + "1.png");
    },

    /**
     * 加载动画
     * @param {float} animation_speed
     */
    loadAnimation: function (animation_speed) {
        this.stopAllActions();
        var frames = [];

        var name, frame;
        var i;
        for (i = 1; i < 10; i++) {
            name = this._name + i + ".png";

            frame = cc.spriteFrameCache.getSpriteFrame(name);
            frames.unshift(frame);
        }
        for (i = 1; i < 5; i++) {
            for (var j = 0; j < 10; j++) {
                if (i == 4 && j > 8) {
                    continue;
                }
                name = this._name + i + j + ".png";
                frame = cc.spriteFrameCache.getSpriteFrame(name);
                frames.unshift(frame);
            }
        }
        var animation = new cc.Animation(frames, animation_speed);
        animation.setRestoreOriginalFrame(true);
        var animate = new cc.animate(animation);
        this.runAction(animate.repeatForever());
    },

    /**
     * 滚动
     * @param {[{x:number,y:number,r:number}]} routes 路径
     * @param {number} step_speed  步/帧
     */
    roll: function (routes, step_speed) {
        this._routes = routes;
        this._step_speed = step_speed;
        this._is_roll = true;
        var animation_speed = step_speed / 40;

        /**
         * 停止滚动，不跟新滚动状态
         */
        this.stopAllActions();
        this.unscheduleAllCallbacks();

        this.loadAnimation(animation_speed);
        this.schedule(this.rollUpdate);
    },

    /**
     * 滚动调度器
     */
    rollUpdate: function () {
        if (this._step < this._routes.length) {
            var route = this._routes[this._step];
            this.x = route.x;
            this.y = route.y;

            if (this._step < 0) {
                this.rotation = -route.r;
            } else {
                this.rotation = route.r;
            }

            this._step = this._step + this._step_speed;
        } else {
            // 游戏结束
        }
    },

    /**
     * 滚动到固定步数
     * @param roll_step 滚动步数
     * @param roll_step_speed 步/帧
     */
    rollOnce: function (roll_step, roll_step_speed) {
        this._roll_step = roll_step;
        this._roll_step_speed = roll_step_speed;
        this._roll_step_count = 0;

        /**
         * 停止滚动，不跟新滚动状态
         */
        this.stopAllActions();
        this.unscheduleAllCallbacks();

        var animation_speed = roll_step_speed / 30;

        this.loadAnimation(animation_speed);
        this.schedule(this.rollOnceUpdate);
    },
    /**
     * 滚动到固定步数调度器
     */
    rollOnceUpdate: function () {
        if (this._step < this._routes.length) {
            var route = this._routes[this._step];
            this.x = route.x;
            this.y = route.y;

            if (this._step < 0) {
                this.rotation = -route.rotation;
            } else {
                this.rotation = route.rotation;
            }
            if (this._roll_step_count >= this._roll_step) {
                this.stopAllActions();
                this.unscheduleAllCallbacks();

                if (this._is_roll) {
                    this.roll(this._routes, this._step_speed);
                }
                return;
            }

            this._roll_step_count = this._roll_step_count + this._roll_step_speed;
            this._step = this._step + this._roll_step_speed + this._step_speed;
        } else {
            // 游戏结束
        }
    },

    /**
     * 停住滚动
     */
    stopRoll: function () {
        this._is_roll = false;
        this.stopAllActions();
        this.unscheduleAllCallbacks();
    },

    /**
     * 平移到某一步
     * @param {number} move_step 位置，步数
     * @param {number} move_speed 多少帧平移到
     * @param {object} call_fun 回调函数
     */
    move: function (move_step, move_speed, call_fun) {
        this._move_route = [];
        this._step = this._step + move_speed * this._step_speed;
        this._move_call_fun = call_fun;

        var nextPos = this._routes[move_step];
        var max_range;

        var radian = Math.atan2(nextPos.x - this.x, nextPos.y - this.y);
        var rotation = 180 / Math.PI * radian;
        rotation = rotation < 0 ? 360 + rotation : rotation;
        if (nextPos.x - this.x != 0) {
            max_range = (nextPos.x - this.x) / Math.sin(radian);
        } else {
            max_range = Math.abs(nextPos.y - this.y);
        }

        for (var i = 0, len = move_speed; i < len; i++) {
            var x = this.x + max_range / move_speed * i * Math.sin(radian);
            var y = this.y + max_range / move_speed * i * Math.cos(radian);
            this._move_route.push({x: x, y: y, r: rotation});
        }

        /**
         * 停止滚动，不跟新滚动状态
         */
        this.stopAllActions();
        this.unscheduleAllCallbacks();

        var animation_speed = 1 / 30;

        this.loadAnimation(animation_speed);
        this.schedule(this.moveUpdate);
    },

    moveUpdate: function () {
        var route = this._move_route.shift();
        this.x = route.x;
        this.y = route.y;

        this.rotation = route.rotation;

        if (!this._move_route.length) {
            this.stopAllActions();
            this.unscheduleAllCallbacks();

            if (this._is_roll) {
                this.roll(this._routes, this._step_speed);
            }
            gameLayer._turretSprite._isRadiated = true;
            if (typeof this._move_call_fun == 'function') {
                cc.log(this._step)
                this._move_call_fun.call();
            }

        }
    }


});